﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EMS.Calendar.Models;

namespace EMS.Calendar.Services {
    public static class StaticCalendarServices {
        public static List<DayNameType> GetDayNameListFromArray(string[] dayNames) {
            List<DayNameType> dayNameTypes = new List<DayNameType>();

            foreach (var dayName in dayNames) {
                DayNameType tempDayNameType = DayNameType.Sunday;
                if (Enum.TryParse<DayNameType>(dayName.Trim(), out tempDayNameType)) {
                    dayNameTypes.Add(tempDayNameType);
                }
            }

            return dayNameTypes;
        }

        public static string[] GetDayNameArrayFromList(List<DayNameType> dayNameTypes) {
            string[] arrayString = new string[dayNameTypes.Count];

            for (int i = 0; i < dayNameTypes.Count; i++) {
                arrayString[i] = dayNameTypes[i].ToString();
            }

            return arrayString;
        }

        public static List<DayNameType> GetDayNameListFromCSV(string dayNamesString) {
            List<DayNameType> dayNameTypes = new List<DayNameType>();

            if (!String.IsNullOrEmpty(dayNamesString)) {
                string[] dayNames = dayNamesString.Split(',');
                foreach (var dayName in dayNames) {
                    DayNameType tempDayNameType = DayNameType.Sunday;
                    if (Enum.TryParse<DayNameType>(dayName.Trim(), out tempDayNameType)) {
                        dayNameTypes.Add(tempDayNameType);
                    }
                }
            }

            return dayNameTypes;
        }

        public static string GetDayNameCSVFromList(List<DayNameType> dayNameTypes) {
            string csvString = "";

            foreach (var dayNameType in dayNameTypes) {
                csvString += String.Format("{0},", dayNameType.ToString());
            }

            if (dayNameTypes.Count > 0) {
                // Trim off the last ","
                csvString = csvString.Substring(0, csvString.Length - 1);
            }

            return csvString;
        }
    }
}